import { EcsSystem, filter, IEntity, IFilter } from "db://pkg/@gamex/cc-ecs";
import { PlayerComponent } from "../component/PlayerComponent";
import { EnemyComponent } from "../component/EnemyComponent";
import { BulletComponent } from "../component/BulletComponent";
import { DestroyComponent } from "../component/DestroyComponent";
import { gameRange } from "../../../Common";

export class ColliderSystem extends EcsSystem {
    private playerFilter = filter.all(PlayerComponent);
    private enemyFilter = filter.all(EnemyComponent).exclude(DestroyComponent);
    private bulletFilter = filter.all(BulletComponent).exclude(DestroyComponent);

    protected execute(): void {
        const playerEntity = this.find(this.playerFilter);
        if (!playerEntity) return;

        const enemyEntities = this.query(this.enemyFilter);
        const bulletEntities = this.query(this.bulletFilter);

        for (const enemyEntity of enemyEntities) {
          if (enemyEntity.node.z > gameRange.maxZ) {
            enemyEntity.add(DestroyComponent);
          }
        }
        for (const bulletEntity of bulletEntities) {
          if (bulletEntity.node.z < gameRange.minZ || bulletEntity.node.z > gameRange.maxZ 
            || bulletEntity.node.x < gameRange.minX || bulletEntity.node.x > gameRange.maxX) {
            bulletEntity.add(DestroyComponent);
          } 
        }
    }

}